package com.me.jingdongshop.ui.activity;

import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.me.common.base.BaseActivity;
import com.me.common.base.Const;
import com.me.common.base.Global;
import com.me.jingdongshop.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by YL on 2017/5/7.
 */

public class BigImageActivity extends BaseActivity {
    private android.support.v4.view.ViewPager bigImageVP;
    private android.widget.TextView bigImageIndicator;
    private ArrayList<String> mBigImg;
    private int mPosition;

    @Override
    public int getLayoutRes() {
        return R.layout.bigimage_activity;
    }

    @Override
    public void initView() {

        bigImageVP = (ViewPager) findViewById(R.id.bigImageVP);
        bigImageIndicator = (TextView) findViewById(R.id.bigImage_indicator);

        //获得数据
        mPosition = getIntent().getIntExtra("position", 0);
        mBigImg = getIntent().getStringArrayListExtra("bigImg");


    }

    @Override
    public void initListener() {
        bigImageVP.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bigImageIndicator.setText((position + 1) + "/" + mBigImg.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {
        //设置适配器
        bigImageVP.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mBigImg.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(Global.mContext);
                Picasso.with(Global.mContext).load(Const.HTTP_IMG + mBigImg.get(position)).into(imageView);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        bigImageVP.setCurrentItem(mPosition);
    }

    @Override
    public void onClick(View v, int id) {

    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {

    }

    @Override
    public void onHttpError(int reqType, String error) {

    }
}
